import bigScreen from "@/api/bigScreen";
import router from "@/router"
import { Message } from "element-ui";

export async function beginSearchPayStatus() {
	global.searchPayCount = 0;
	searchPayStatus()
}

async function searchPayStatus() {
	if (!localStorage.getItem('UUID')) {
		return;
	}
	let UUID = localStorage.getItem('UUID')
	let type = localStorage.getItem('payType')
	let goodId = localStorage.getItem("goodId");
	global.searchPayCount += 1;
	const res = await bigScreen.getBillStatus({ uuId: UUID,type,goodId });
	if (global.searchPayCount > 60) {
		Message({
			message: '支付超时!',
			type: 'success'
		});
		localStorage.removeItem('UUID')
		setTimeout(() => {
			router.push({
				name: 'choseObject'
			})
		}, 800)
		return;
	} else if (res === 'pending') {
		setTimeout(() => {
			searchPayStatus(UUID)
		}, 3000)
	} else if (res) {
		localStorage.removeItem('UUID')
		localStorage.setItem('UUIDNEW',UUID)
		Message({
			message: '支付成功!',
			type: 'success'
		});
		setTimeout(() => {
			let payType = type?type:''
			router.push({
				name: 'payResult',
				query:{
					payType:payType
				}
			})
		}, 800)
		return;
	}
}

export function stopSearchPay() {
	localStorage.removeItem('UUID')
}

export function checkCanTy() {
	if (localStorage.getItem('tyCount') && localStorage.getItem('tyCount') >= 5) {
		Message({
			message: '您没有试用机会了！',
			type: 'warning'
		});
		return false;
	}
	return true;
}

export function addTyCount() {
	if (localStorage.getItem('tyCount')) {
		localStorage.setItem('tyCount', localStorage.getItem('tyCount') + 1)
	} else {
		localStorage.setItem('tyCount', 1)
	}
}

export function getTyCount() {
	if (localStorage.getItem('tyCount')) {
		if (localStorage.getItem('tyCount') > 5) {
			return 0;
		} else {
			return 5 - localStorage.getItem('tyCount')
		}
	}
	return 5;
}

export function IsPC() {
	var userAgentInfo = navigator.userAgent;
	var Agents = ["Android", "iPhone",
		"SymbianOS", "Windows Phone",
		"iPad", "iPod"
	];
	var flagPc = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flagPc = false;
			break;
		}
	}
	return flagPc;
}
